package DemoPackage;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Resource;

import java.io.File;

// 介绍一个基本的Task demo
// 重点介绍了参数类型的转换
public class DemoTask extends Task {

    private boolean varBoolean;
    private char varChar;
    private File varFile;
    private Resource varResource;
    private Path varPath;
    private Class varClass;
    private TestClass varAnyClassInstance;

    @Override
    public void execute() throws BuildException {
        log("Message: " + "Parameter varBoolean is " + this.varBoolean, Project.MSG_INFO);
        log("Message: " + "Parameter varChar is " + this.varChar, Project.MSG_INFO);
        log("Message: " + "Parameter varFile is " + this.varFile, Project.MSG_INFO);
        log("Message: " + "Parameter varResource is " + this.varResource, Project.MSG_INFO);
        log("Message: " + "Parameter varPath is " + this.varPath, Project.MSG_INFO);
        log("Message: " + "Parameter varClass is " + this.varClass, Project.MSG_INFO);
        log("Message: " + "Parameter varAnyClassInstance is " + this.varAnyClassInstance, Project.MSG_INFO);
    }

    // ***Ant针对不同的参数类型会自动进行转换***

    // boolean: 如果在构建文件中指定的值是"true"，"yes"或"on"则会转换为true, 否则为false
    public void setVarBoolean(boolean varBoolean) {
        this.varBoolean = varBoolean;
    }
    // char: 传入构建文件中指定值的第一个字符
    public void setVarChar(char varChar) {
        this.varChar = varChar;
    }
    // java.io.File: 传入指定路径的文件对象（构建文件如果指定的是相对路径则是相对于构建文件所在目录）
    public void setVarFile(File varFile) {
        this.varFile = varFile;
    }
    // org.apache.tools.ant.types.Resource: 和上面类似，最终转换为Resource对象
    public void setVarResource(Resource varResource) {
        this.varResource = varResource;
    }
    // org.apache.tools.ant.types.Path: 接受使用":"和";"分隔符的多个路径字符串并转换为Path对象
    public void setVarPath(Path varPath) {
        this.varPath = varPath;
    }
    // java.lang.Class: 将构建文件中给出的值解释为Java类名称，然后从系统类加载器加载命名的类（ant/lib/目录下的jar包）
    public void setVarClass(Class varClass) {
        this.varClass = varClass;
    }
    // 任何一个带有单个String参数的构造函数的类型，Ant将根据构建文件中的值创建一个新的实例
    public void setVarAnyClassInstance(TestClass varAnyClassInstance) {
        this.varAnyClassInstance = varAnyClassInstance;
    }
}

